﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.InterferenceVolumeQuery.Tree.TreeListItem
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System.ComponentModel;
using System.Windows.Controls;
using System.Windows.Input;


namespace DnProcessSimulateCommands.InterferenceVolumeQuery.Tree;

public class TreeListItem : ListViewItem, INotifyPropertyChanged
{
  private const int LIST_VIEW_ITEM_HEIGHT = 19;
  private TreeNode _node;

  public TreeNode Node
  {
    get => this._node;
    internal set
    {
      this._node = value;
      this.OnPropertyChanged(nameof (Node));
    }
  }

  public TreeListItem() => this.Height = 19.0;

  protected override void OnKeyDown(KeyEventArgs e)
  {
    if (this.Node != null)
    {
      switch (e.Key)
      {
        case Key.Left:
          e.Handled = true;
          if (this.Node.IsExpanded && this.Node.IsExpandable)
          {
            this.Node.IsExpanded = false;
            this.ChangeFocus(this.Node);
            break;
          }
          this.ChangeFocus(this.Node.Parent);
          break;
        case Key.Right:
          e.Handled = true;
          if (!this.Node.IsExpanded)
          {
            this.Node.IsExpanded = true;
            this.ChangeFocus(this.Node);
            break;
          }
          if (this.Node.Children.Count > 0)
          {
            this.ChangeFocus(this.Node.Children[0]);
            break;
          }
          break;
        case Key.Add:
          e.Handled = true;
          this.Node.IsExpanded = true;
          this.ChangeFocus(this.Node);
          break;
        case Key.Subtract:
          e.Handled = true;
          this.Node.IsExpanded = false;
          this.ChangeFocus(this.Node);
          break;
      }
    }
    if (e.Handled)
      return;
    base.OnKeyDown(e);
  }

  private void ChangeFocus(TreeNode node)
  {
    TreeList tree = node.Tree;
    if (tree == null)
      return;
    if (tree.ItemContainerGenerator.ContainerFromItem((object) node) is TreeListItem treeListItem)
      treeListItem.Focus();
    else
      tree.PendingFocusNode = node;
  }

  public event PropertyChangedEventHandler PropertyChanged;

  private void OnPropertyChanged(string name)
  {
    if (this.PropertyChanged == null)
      return;
    this.PropertyChanged((object) this, new PropertyChangedEventArgs(name));
  }
}
